<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="stylesheet" href="css/registerCommon.css">
</head>
<body>
    <div class="nav">电子物品交易</div>
    <div class="container">
      <!-- 登录对话框 -->
      <div class="dialog">
         <h2>用户登录</h2>

         <div class="row">
            <span>邮箱</span>
            <input type="text" id="email" placeholder="请输入邮箱">
         </div>

         <div class="row">
            <span>密码</span>
            <input type="password" id="password" placeholder="请输入密码">
         </div>
          <div class="row">
              <span>验证码</span>
              <input name="verifyCode" id="verifyCode" placeholder="请输入验证码" type="text" >
          </div>
          <div class="row">
              <img title="单击图片刷新！" id="imgCode" src="/util/verifycode" onclick="this.src='/util/verifycode'"/>
          </div>
         <div class="row">
            <button class="submit" onclick="login()">提交</button>
         </div>

      </div>

    </div>

    <script src="js/jquery.min.js"></script>
    <script>
      function login() {
          if($('#email').val() == null || $('#email').val() === "") {
              alert("邮箱不能为空");
              return;
          }
          if($('#password').val() == null || $('#password').val() === "") {
              alert("密码不能为空");
              return;
          }
          if($('#verifyCode').val() == null || $('#verifyCode').val() === "") {
              alert("验证码不能为空");
              return;
          }
         $.ajax({
            type: 'post',
            url: '/user/login',
            data: {
               email: $("#email").val(),
               password: $("#password").val(),
               verifyCode: $("#verifyCode").val()
            }
            ,success: function(result) {
               if(result.code === "SUCCESS") {
                   localStorage.setItem("userId", result.data.userId);
                   console.log(result.data.userType);
                   if(result.data.userType === "管理员") {
                       location.href = "/adminHall.html";
                   }else{
                       location.href = "/hall.html";
                   }
               }else{
                   alert(result.errMsg);
                   $("#imgCode").click();
               }
            }
            ,error: function() {
               alert("请求发送失败");
            }
         });
      }
    </script>
</body>
</html>